package com.yunyike.app.service;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.rop.annotation.IgnoreSignType;
import com.rop.annotation.NeedInSessionType;
import com.rop.annotation.ServiceMethod;
import com.rop.annotation.ServiceMethodBean;
import com.rop.response.Response;
import com.yunyike.app.constants.CommonConstant;
import com.yunyike.app.constants.OrderConstants;
import com.yunyike.app.data.dto.UserOrdersDTO;
import com.yunyike.app.data.model.User;
import com.yunyike.app.data.service.IOrderService;
import com.yunyike.app.data.service.IUserService;
import com.yunyike.app.redis.JedisManager;
import com.yunyike.app.request.OrderRequest;
import com.yunyike.app.request.PayRequest;

@ServiceMethodBean(ignoreSign = IgnoreSignType.NO)
public class APPOrderService {

    private Logger log = LoggerFactory.getLogger(APPOrderService.class);
    
    @Autowired
	private IOrderService orderService;
    @Autowired
   	private  IUserService userService;
    
	@ServiceMethod(method = "userorder.getunpaid", version = "1.0", needInSession = NeedInSessionType.NO)
	public Response getUnpaidOrder(OrderRequest orderRequest) {

		Response response = new Response();
		String shopperId = orderRequest.getShopperId();
		String shopId = orderRequest.getShopId();
		String orderId = orderRequest.getOrderId();
		String msg = "";
		log.info("userorder--getunpaid::shopperId=" + shopperId + ",shopId=" + shopId+",orderId="+orderId);
		try {
			User user = JedisManager.getObject(shopperId + CommonConstant.SHOPPER_CONNECT_USER, User.class);
			log.info("cart--getUserCart--jedis。get。user::user=" + user);
			if (user == null) {
				msg = "参数错误，userId获取不到用户";
				response.getResult().setResult("0");
			} else {

				UserOrdersDTO dto = new UserOrdersDTO();
				dto.setUserId(user.getUserId());
				dto.setShopId(shopId);
				dto.setOrderStatus(OrderConstants.ORDER_STATUS_0);
				if (orderId != null && !"".equals(orderId))
					dto.setOrderId(orderId);
				List<UserOrdersDTO> userOrders = orderService.findUserOrders(dto);

				Map<String, Object> data = new HashMap<String, Object>(1);
				data.put("UserOrders", userOrders);
				msg = "获取待支付订单成功";
				response.getResult().setResult("1");
				response.setData(data);
				if (orderId != null && !"".equals(orderId)) {
					if (userOrders == null || userOrders.size() == 0) {
						response.getResult().setResult("0");
						msg = "未搜索到该待支付订单";
					}
				}
			}
			
		} catch (Exception e) {
			response.getResult().setResult("0");
			msg = e.getMessage();
			e.printStackTrace();
		}
		response.getResult().setMessage(msg);
		log.info("userorder--getunpaid::msg=" + msg);
		return response;
	}
	
	@ServiceMethod(method = "userorder.payorder", version = "1.0", needInSession = NeedInSessionType.NO)
	public Response payOrder(PayRequest payRequest) {

		Response response = new Response();
		String shopperId = payRequest.getShopperId();
		String orderId = payRequest.getOrderId();
		String msg = "";
		log.info("userorder--payorder::shopperId=" + shopperId + ",orderId=" + orderId);
		try {
			User user = JedisManager.getObject(shopperId + CommonConstant.SHOPPER_CONNECT_USER, User.class);
			log.info("cart--getUserCart--jedis。get。user::user=" + user);
			if (user == null) {
				msg = "参数错误，userId获取不到用户";
				response.getResult().setResult("0");
			} else {

				UserOrdersDTO dto = new UserOrdersDTO();
				dto.setUserId(user.getUserId());
				dto.setOrderId(orderId);

				String result = orderService.updateOrderStatu(dto, OrderConstants.ORDER_STATUS_1);
				if (result == null) {
					msg = "支付成功";
					response.getResult().setResult("1");

				} else {
					msg = result;
					response.getResult().setResult("0");
				}

			}
			
		} catch (Exception e) {
			response.getResult().setResult("0");
			msg = e.getMessage();
			e.printStackTrace();
		}
		response.getResult().setMessage(msg);
		log.info("userorder--payorder::msg=" + msg);
		return response;
	}
	
	
}

